<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\UserAssignable */

$this->title = '为"'.$model->person->name.'"分配可用的角色';
$this->params['breadcrumbs'][] = ['label' => '可用角色分配', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
$this->registerCssFile('@web/css/user_log.css');
?>
<div class="role-assignable-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <a href="javascript:;" class="btn btn-primary" id="btn_pass">通过</a>

        <?= Html::a('拒绝申请', ['refuse', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => '确定要拒绝申请吗？',
                'method' => 'post',
            ],
        ]) ?>
    </p>
    <?

    $assign_opt = '<div class="avaliable_role_assgin">
        <select size="8" multiple id="available_sel">

        </select>
        <div class="assgin_btn_grp">

            <button id="assign_btn">&gt;&gt;</button>
            <button id="del_btn">&lt;&lt;</button>
        </div>
        <select size="8" multiple id="assigned_sel">
        </select>
    </div>';
    $num_input = Html::input('text','assign_num');
    ?>





    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            [
                'attribute'=>'账号名',
                'value' =>$model->company->code,
            ],
            [
                'attribute'=>'密码',
                'value'=>$model->psw_tmp,
            ],
            [
                'attribute'=>'可分配角色',
                'format' => 'raw',
                'value'=>$assign_opt,

            ],
            [
                'attribute'=>'可分配账号数',
                'format' => 'raw',
                'value'=>$num_input,

            ],

        ],
    ]) ?>

</div>
<?php
$js = <<<JS
$(function(){
    var id = $model->id;
    getroles(id);
    $("#assign_btn").click(function(){
        $("#available_sel option:selected").each(function(){
            $("#assigned_sel").append('<option>'+$(this).text()+'</option>');
            $(this).remove();
        })
    })

    $("#del_btn").click(function(){
        $("#assigned_sel option:selected").each(function(){
            $("#available_sel").append('<option>'+$(this).text()+'</option>');
            $(this).remove();
        })
    })
    $("#btn_pass").click(function(){
        if(!confirm("确定要通过改审核吗？（账号信息将以邮件形式发送给用户）")){
            return;
        }
        var assign_num = $("input[name='assign_num']").val();
        var assgined = [];
        $("#assigned_sel option").each(function(i,e){
            assgined.push($(this).text());
        })
        document.location.href="index.php?r=backend/user-log/pass&id="+id+"&assign_num="+assign_num+"&assigned="+assgined;
        /*$.ajax({
            type:get,
            url:'index.php?r=backend/user-log/pass',
            data:{
                'id':id,
                'assign_num':assign_num,
                'assgined':assgined
            },
            dataType:text,
            success:function(msg){
                alert(msg)
            },
            error:function(msg){
                alert("服务器访问错误");
            }
        })*/

    })
})

function getroles(id){
    $.ajax({
        type:'get',
        url:'index.php?r=backend/user-log/getroles',
        data:{
            'id':id
        },
        dataType:'json',
        success:function(msg){
            var la = msg.available.length;
            var lb = msg.assigned.length;
            var available = msg.available;
            var assigned = msg.assigned;
            $("#available_sel").empty();
            $("#assigned_sel").empty();
            for(var i=0;i<la;i++){
                $("#available_sel").append('<option>'+available[i]+'</option>');
            }
            for(var i=0;i<lb;i++){
                $("#assigned_sel").append('<option>'+assigned[i]+'</option>');
            }
        },
        error:function(msg){
            alert("服务器访问出错！");
        }
    })
}

JS;
$this->registerJs($js);
?>